// order_detail.js
var app = getApp()
Page({

  data: {
      statusName: ['未付款', '已付款', '已出货', '已完成', '拼团成功', '拼团失败', '退款成功']
  },
  goodsClick : function(e){
    var id = e.currentTarget.dataset.id

    wx.redirectTo({
      url: '../goodsDetail/goodsDetail?goods_id= ' + id,
    })
  },
  //付款
  pay_bottom : function(){
    var thiz = this
    var domain = app.globalData.domain
    var user_id = app.globalData.inv_i
    var payUrl = app.globalData.PageInterface

    wx.request({
      url: domain + payUrl,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Order',
        model: "MINI",
        api: 8008,
        user_id: user_id,
        order_sn: thiz.data.order_id
      },
      success: function (res) {
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success: function (res) {
            wx.showToast({
              title: '支付成功'
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '支付失败'
            })
          },
          complete: function (re) {
            wx.redirectTo({
              url: '../order_detail/order_detail?order_id=' + thiz.data.order_id,
            })
          }
        })
      }
    })
  },
  formatDate : function(str) {
    var now = new Date(parseInt(str) * 1000);
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "年" + this.fixZero(month, 2) + "月" + this.fixZero(date, 2) + "日" + this.fixZero(hour, 2) + ":" + this.fixZero(minute, 2) + ":" + this.fixZero(second, 2);
  },
  fixZero : function(num, length) {
    var str = "" + num;
    var len = str.length; var s = "";
    for (var i = length; i-- > len;) {
      s += "0";
    }
    return s + str;
  }, 
  goToIndex: function () {
    wx.switchTab({
      url: '../index/index',
    })
  },
  onLoad: function (options) {
    var thiz = this

    var background_color = app.globalData.background_color

    wx.setNavigationBarColor({
      backgroundColor: background_color,
    })

    wx.getSystemInfo({
      success: function(res) {
        thiz.setData({
          windowHeight : res.windowHeight,
          windowWidth : res.windowWidth
        })
      },
    })    

    thiz.setData({
      order_id : options.order_id,
      hiddenLoading : false
    })
  
    var order_id = thiz.data.order_id

    var cid = app.globalData.cid
    var kui_key = app.globalData.kui_key
    var domain = app.globalData.domain
    var domainImg = app.globalData.domain
    var orderDetail = app.globalData.orderDetailInterface
    var orderDetailArray = []
    var user_id = app.globalData.inv_i

    wx.request({
      url: domain + orderDetail,
      data: {
        format_type: 'json',
        api_cate: 'Order',
        api: 8002,
        order_id: order_id,
        model: "MINI",
        user_id: user_id
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        var goods_detail = app.globalData.goodsDetailInterface
        var goods_spec

        wx.request({
          url: domain+goods_detail,
          data:{
            format_type: 'json',
            api_cate: 'Goods',
            api: 4001,
            goods_id: res.data.data.goods_id,
            c_id: res.data.data.c_id,
            model: "MINI",
            user_id: user_id
          },
          method: 'POST',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success:function(goods){
              console.log(goods);
              goods.data.data.default_img = domainImg + goods.data.data.default_img
              var addressDetail = app.globalData.addrInfoInterface
              wx.request({
                url: domain+addressDetail,
                data:{
                  format_type: 'json',
                  api_cate: 'Personal',
                  api: 7003,
                  address_id: res.data.data.address_id,
                  model: "MINI",
                  user_id: user_id
                },
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success:function(address){
                  console.log(address)
                  var obj = {
                    full_name : address.data.data.address_name,
                    tel : address.data.data.tel,
                    addr : address.data.data.address,
                    goods : goods.data.data,
                  }
                  orderDetailArray.push(obj)
                  thiz.setData({
                    order_statusText: res.data.data.status == 1 ? '已付款' : '未付款',
                    order_sn: res.data.data.order_sn,
                    order_paytime: thiz.formatDate(res.data.data.pay_time),
                    order_status: res.data.data.status,
                    order_message: res.data.data.message == undefined ? '无' : res.data.data.message,
                    order_payMoney: res.data.data.total_price,
                    allData: res.data.data,
                    orderDetailArray: orderDetailArray,
                    hiddenLoading: true
                  })
                }
              })
          }
        })
        //res.data.option_detail.forEach(function (element, index) {
          // wx.request({
          //   url: domain + goods_detail,
          //   data: {
          //     cid: cid,
          //     kui_key: kui_key,
          //     id: element.goods_id
          //   },
          //   method: 'POST',
          //   header: { 'content-type': 'application/x-www-form-urlencoded' },
          //   success: function (goods) {
          //     goods.data.all_spec.forEach(function (e, zindex) {
          //       if (e.goods_option_id == element.goods_spec_id) {
          //         goods_spec = e
          //       }
          //     })
          //     goods.data.default_img = domain + goods.data.default_img
          //     var obj = {
          //       full_name: element.full_name,
          //       tel: element.tel,
          //       addr: element.addr,
          //       goods: goods.data,
          //       amount: element.amount,
          //       goods_spec: goods_spec
          //     }
          //     orderDetailArray.push(obj)

          //     thiz.setData({
          //       order_statusText: res.data.status == 1 ? '已付款' : '未付款',
          //       order_sn: res.data.order_sn,
          //       order_paytime: thiz.formatDate(res.data.pay_time),
          //       order_status: res.data.status,
          //       order_message: res.data.message == 'undefined' ? '无' : res.data.message,
          //       order_payMoney: res.data.pay_price,
          //       allData: res.data,
          //       orderDetailArray: orderDetailArray,
          //       hiddenLoading: true
          //     })
          //   }
          // })

        //})
      }
    })

    // this.setData({
    //   order_id : options.order_id,
    //   addr : JSON.parse(options.addr),
    //   orderDetailArray : []
    // })

  },
})